
package com.snail.mobile.sim.oss.userInfo.service.impl;

import java.util.Collection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;

import com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao;
import com.snail.mobile.sim.oss.userInfo.model.BillingPlatformParam;
import com.snail.mobile.sim.oss.userInfo.model.UserInfo;
import com.snail.mobile.sim.oss.userInfo.model.UserInfoResult;
import com.snail.mobile.sim.oss.userInfo.service.UserInfoManager;
import com.snail.mobile.sim.rest.RestClient;
import com.sungrid.seine.oss.core.system.dao.SysLogDao;
import com.sungrid.seine.oss.framework.service.impl.BaseManagerImpl;
import com.sungrid.seine.oss.framework.util.StringHelper;

public class UserInfoManagerImpl extends BaseManagerImpl implements UserInfoManager 
{
    private UserInfoDao dao;
    private SysLogDao sysLogDao;
    
	private RestClient restClient;
	

	private BillingPlatformParam billingPlatformParam;

    public RestClient getRestClient() {
		return restClient;
	}

	public void setRestClient(RestClient restClient) {
		this.restClient = restClient;
	}

	public BillingPlatformParam getBillingPlatformParam() {
		return billingPlatformParam;
	}

	public void setBillingPlatformParam(BillingPlatformParam billingPlatformParam) {
		this.billingPlatformParam = billingPlatformParam;
	}

	/**
     * Set the Dao for communication with the data layer.
     * @param dao
     */
    public void setUserInfoDao(UserInfoDao dao) 
    {
        this.dao = dao;
    }

    /**
     * Set the Log Dao for communication with the data layer.
     * 
     * @param dao
     */
    public void setSysLogDao(SysLogDao dao)
    {
        this.sysLogDao = dao;
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoManager#getUserInfos(com.snail.mobile.sim.oss.userInfo.model.UserInfo)
     */
    public List getUserInfos(final UserInfo userInfo) 
    {
        return dao.getUserInfos(userInfo);
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoManager#getUserInfo(String nid)
     */
    public UserInfo getUserInfo(final String nid) 
    {
        return dao.getUserInfo(new Integer(nid));
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoManager#getUserInfo(String nid)
     */
    public UserInfo getUserInfoSimple(final String nid) 
    {
        return dao.getUserInfoSimple(new Integer(nid));
    }
    
    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoManager#saveUserInfo(UserInfo userInfo)
     */
    public void saveUserInfo(UserInfo userInfo) 
    {
        if (StringHelper.isNotNull(userInfo.getNid()))
        {
            dao.saveOrUpdateUserInfo(userInfo);
        }
        else
        {
            dao.saveUserInfo(userInfo);
        }
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoManager#removeUserInfo(String nid)
     */
    public void removeUserInfo(final String nid) 
    {
        dao.removeUserInfo(new Integer(nid));
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoDao#removeUserInfos(Collection userInfos)
     */
    public void removeUserInfos(final Collection userInfos)
    {
        dao.removeUserInfos(userInfos);
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.service.UserInfoDao#saveUserInfos(Collection userInfos)
     */
    public void saveUserInfos(final Collection userInfos)
    {
        dao.saveUserInfos(userInfos);
    }
    
    
    /*
     * 调用计费接口获取账号是否存在
     */
    public UserInfoResult getUserInfoFromBilling(String opIp,String sphone){
    	UserInfoResult userInfoResult=new UserInfoResult();
    	LinkedMultiValueMap<String, String> mapHeaders = getHeaders(opIp);
		LinkedMultiValueMap<String, Object> mapPostData = getPostQueryData(sphone);
		ResponseEntity<String> strResult;
		strResult = (ResponseEntity<String>) this.restClient.postEntity(
				"/user/passport/query", mapHeaders, mapPostData, String.class);
		JSONObject jsonObject = JSONObject.fromObject(strResult.getBody());
		String state = jsonObject.getString("STATE");
		if ("0".equals(
				StringUtils.defaultString(state))) {
			userInfoResult.setState(0);
			return userInfoResult;
		}
		userInfoResult.setState(1);
		userInfoResult.setCode(jsonObject.getString("CODE"));
		userInfoResult.setDesc(jsonObject.getString("DESC"));

		return userInfoResult;

    }
    private LinkedMultiValueMap<String, String> getHeaders(String ip) {

		LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
		headers.add("H_APPID", billingPlatformParam.getAppId());
		headers.add("H_PWD", billingPlatformParam.getPwd());
		headers.add("Content-Type", billingPlatformParam.getContentType());
		headers.add("H_UIP", ip);
		return headers;

	}

	private LinkedMultiValueMap<String, Object> getPostQueryData(String sphone) {

		LinkedMultiValueMap<String, Object> datas = new LinkedMultiValueMap<String, Object>();
		datas.add("account", sphone+billingPlatformParam.getPhoneSuffix());
		return datas;
	}
}
